package gui;

import gyroscope.Gyroscope;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.media.opengl.GLJPanel;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JSplitPane;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import saite.U1_SchwingendeSaite;
import saite.U1_SchwingendeSaite.MODE;
import stroemung.Stroemung;
import stroemung.Stroemung.Mode;
import wuerfel.U2_Wuerfel;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.BoxLayout;

import pendel.KugelPendel;

import boomerang.U3_Boomerang;
import boomerang.U3_Boomerangv2;

public class DemoGui2 implements ActionListener{

	private JFrame frame;
	private U1_SchwingendeSaite 	U1;
	private U2_Wuerfel 				U2;
	private U3_Boomerang 			U3;
	private U3_Boomerangv2 			U3v2;
	private KugelPendel 			U4;
	private Stroemung 				U5;
	private gyroscope2.Gyroscope 	U6;
	private GLJPanel opengl;
	private JSplitPane splitPane;
	private JPanel controls;
	private JPanel statusPane;
	private static JLabel statusText;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DemoGui2 window = new DemoGui2();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DemoGui2() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("pfcs: Übungen");
		frame.setBounds(100, 100, 800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setMinimumSize(new Dimension(320, 240));
		
		statusPane = new JPanel();
		statusText = new JLabel("      ");
		statusPane.add(statusText);
		
		frame.getContentPane().add(statusPane, BorderLayout.SOUTH);
			
		splitPane = new JSplitPane();
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		controls = new JPanel();
		splitPane.setLeftComponent(controls);
		controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
		
				
		JPanel placeholder = new JPanel();
		placeholder.setLayout(new BorderLayout());
		JLabel statusText2 = new JLabel("Bitte Übung auswählen");
		placeholder.add(statusText2,BorderLayout.CENTER);
		splitPane.setRightComponent(placeholder);
		
		
		splitPane.setDividerLocation(200);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("Datei");
		menuBar.add(mnFile);
		
		JMenuItem mntmQuit = new JMenuItem("Beenden");
		mntmQuit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (opengl!=null){opengl.setEnabled(false);}
				System.exit(0);
			}
		});
		mnFile.add(mntmQuit);
		
		JMenu mnbungen = new JMenu("\u00DCbungen");
		menuBar.add(mnbungen);
		
		JMenuItem mntmbungSaite = new JMenuItem("\u00DCbung 1: Schwingende Saite");
		mntmbungSaite.setActionCommand("Uebung1");
		mntmbungSaite.addActionListener(this);
		mnbungen.add(mntmbungSaite);
		
		JMenuItem mntmbung = new JMenuItem("\u00DCbung 2: Fliegender Wuerfel");
		mntmbung.setActionCommand("Uebung2");
		mntmbung.addActionListener(this);
		mnbungen.add(mntmbung);
		
		JMenuItem mntmbung_1 = new JMenu("\u00DCbung 3: Boomerang");
		mnbungen.add(mntmbung_1);
		
		JMenuItem mntmbung_11 = new JMenuItem("1. Version");
		JMenuItem mntmbung_12 = new JMenuItem("2. Version");
		mntmbung_11.setActionCommand("Uebung3v1");
		mntmbung_12.setActionCommand("Uebung3v2");
		mntmbung_11.addActionListener(this);
		mntmbung_12.addActionListener(this);
		mntmbung_1.add(mntmbung_11);
		mntmbung_1.add(mntmbung_12);
		
		
		JMenuItem mntmbung_2 = new JMenuItem("\u00DCbung 4: Pendel");
		mntmbung_2.setActionCommand("Uebung4");
		mntmbung_2.addActionListener(this);
		mnbungen.add(mntmbung_2);
		
		JMenuItem mntmStrmung = new JMenuItem("\u00DCbung 5: Str\u00F6mung");
		mntmStrmung.setActionCommand("Uebung5");
		mntmStrmung.addActionListener(this);
		
		mnbungen.add(mntmStrmung);
		
		JMenuItem mntmbung_3 = new JMenuItem("\u00DCbung 6: Gyroskop");
		mntmbung_3.setActionCommand("Uebung6");
		mntmbung_3.addActionListener(this);
		mnbungen.add(mntmbung_3);
		
	}
	
	
	private String[] ConvertPropertytoSlider(DoubleProperty dp, int precision){
		
		Double tmpMin = dp.getMin() * precision;
		Double tmpMax = dp.getMax() * precision;
		Double tmpCur = dp.getCurrent() * precision;
		String tmpMinstr = tmpMin.toString();
		String tmpMaxstr = tmpMax.toString();
		String tmpCurstr = tmpCur.toString();
		
		tmpMinstr = tmpMinstr.substring(0, tmpMinstr.indexOf("."));
		tmpMaxstr = tmpMaxstr.substring(0, tmpMaxstr.indexOf("."));
		tmpCurstr = tmpCurstr.substring(0, tmpCurstr.indexOf("."));
		
		String[] res = {tmpMinstr,tmpMaxstr,tmpCurstr};
		return res;
	}
	
	public static void setStatusText(String msg){
		statusText.setText(msg);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		statusText.setText(" ");
		controls.removeAll();
		if (arg0.getActionCommand().equals("Uebung5")){
			opengl = new GLJPanel();
			U5 = new Stroemung(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU5();
			splitPane.setDividerLocation(200);
		}
		else if (arg0.getActionCommand().equals("Uebung1")){
			opengl = new GLJPanel();
			U1 = new U1_SchwingendeSaite(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU1();
			statusText.setText("Linearkombination");
			
		}

		else if (arg0.getActionCommand().equals("Uebung2")){
			opengl = new GLJPanel();
			U2 = new U2_Wuerfel(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU2();
		}

		else if (arg0.getActionCommand().equals("Uebung3v1")){
			opengl = new GLJPanel();
			U3 = new U3_Boomerang(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU3v1();
		}
		
		else if (arg0.getActionCommand().equals("Uebung3v2")){
			opengl = new GLJPanel();
			U3v2 = new U3_Boomerangv2(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU3v2();
		}

		else if (arg0.getActionCommand().equals("Uebung4")){
			opengl = new GLJPanel();
			U4 = new KugelPendel(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU4();
		}
		
		else if (arg0.getActionCommand().equals("Uebung6")){
			opengl = new GLJPanel();
			U6 = new gyroscope2.Gyroscope(opengl);
			splitPane.setRightComponent(opengl);
			this.addControlsU6();
		}
		
	}
	
	public void addControlsU1(){
		if (U1 !=null){
			JPanel modeSwitchPanel = new JPanel();
			modeSwitchPanel.setLayout(new GridLayout(7, 0));
			JButton startB = new JButton("Start");
			startB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U1.start();
				}
			});
			JButton stopB = new JButton ("Stop");
			stopB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U1.stop();
				}
			});
			JButton gsb = new JButton("Grundschwingung");
			gsb.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U1.modeSwtich(MODE.GS);
					statusText.setText("Grundschwingung");
					
				}
			});
			JButton o1b = new JButton("Erste Oberschwingung");
			o1b.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U1.modeSwtich(MODE.O1);
					statusText.setText("Erste Oberschwingung");
				}
			});
			JButton o2b = new JButton("Zweite Oberschwingung");
			o2b.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U1.modeSwtich(MODE.O2);
					statusText.setText("Zweite Oberschwingung");
				}
			});
			final JButton linearkom = new JButton("Linearkombination");
			linearkom.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U1.modeSwtich(MODE.LINEARKOM);
					statusText.setText("Linearkombination");
				}
			});
			modeSwitchPanel.add(startB);
			modeSwitchPanel.add(stopB);
			modeSwitchPanel.add(gsb);
			modeSwitchPanel.add(o1b);
			modeSwitchPanel.add(o2b);
			modeSwitchPanel.add(linearkom);
			controls.add(modeSwitchPanel);
			controls.add(Box.createVerticalGlue());
			
		}
	}
	
	public void addControlsU2(){
		if (U2 !=null){
			for(DoubleProperty p : U2.getProperties()){
				
				String[] pslider = this.ConvertPropertytoSlider(p,p.getConversionValue());
				JLabel label = new JLabel(p.getDescription());
				controls.add(label);
				
				JSlider slider = new JSlider(Integer.parseInt(pslider[0]),Integer.parseInt(pslider[1]),Integer.parseInt(pslider[2]));
				slider.setMinorTickSpacing(10);
				slider.addChangeListener(p);
				
				slider.setPaintLabels(true);
				controls.add(slider);
			
			}
			JPanel startstopPanel = new JPanel();
			startstopPanel.setLayout(new GridLayout(11, 0));
			JButton startB = new JButton("Start");
			startB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U2.start();
				}
			});
			JButton stopB = new JButton ("Stop");
			stopB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U2.stop();
				}
			});
			JButton tballb = new JButton ("Tennisball");
			tballb.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U2.shootTennisBall();
				}
			});
			JButton cubeB = new JButton("Wuerfel");
			cubeB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U2.shootWuerfel();
				}
			});
			JButton toggleDrehAchse = new JButton("Drehachse an/aus");
			toggleDrehAchse.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U2.ToggleDrehAchse();
				}
			});
			startstopPanel.add(tballb);
			startstopPanel.add(cubeB);
			startstopPanel.add(toggleDrehAchse);
			controls.add(startstopPanel);
			controls.add(Box.createVerticalGlue());
		}
	}
	
	private void addControlsU3v1() {
		if (U3 !=null){
			for(DoubleProperty p : U3.getProperties()){
				
				String[] pslider = this.ConvertPropertytoSlider(p,p.getConversionValue());
				JLabel label = new JLabel(p.getDescription());
				controls.add(label);
				
				JSlider slider = new JSlider(Integer.parseInt(pslider[0]),Integer.parseInt(pslider[1]),Integer.parseInt(pslider[2]));
				slider.setMinorTickSpacing(10);
				slider.addChangeListener(p);
				
				slider.setPaintLabels(true);
				controls.add(slider);
			
			}
			JPanel startstopPanel = new JPanel();
			startstopPanel.setLayout(new GridLayout(11, 0));
			JButton startB = new JButton("Start");
			startB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U3.start();
				}
			});
			JButton stopB = new JButton ("Stop");
			stopB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U3.stop();
				}
			});
			startstopPanel.add(startB);
			startstopPanel.add(stopB);
			controls.add(startstopPanel);
			controls.add(Box.createVerticalGlue());
		}
	}
		
		private void addControlsU3v2() {
			if (U3v2 !=null){
				for(DoubleProperty p : U3v2.getProperties()){
					
					String[] pslider = this.ConvertPropertytoSlider(p,p.getConversionValue());
					JLabel label = new JLabel(p.getDescription());
					controls.add(label);
					
					JSlider slider = new JSlider(Integer.parseInt(pslider[0]),Integer.parseInt(pslider[1]),Integer.parseInt(pslider[2]));
					slider.setMinorTickSpacing(10);
					slider.addChangeListener(p);
					
					slider.setPaintLabels(true);
					controls.add(slider);
				
				}
				JPanel startstopPanel = new JPanel();
				startstopPanel.setLayout(new GridLayout(11, 0));
				JButton startB = new JButton("Start");
				startB.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						U3v2.start();
					}
				});
				JButton stopB = new JButton ("Stop");
				stopB.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						U3v2.stop();
					}
				});
				startstopPanel.add(startB);
				startstopPanel.add(stopB);
				controls.add(startstopPanel);
				controls.add(Box.createVerticalGlue());
			}
		
	}

	public void addControlsU4(){
		if (U4 !=null){
			for(DoubleProperty p : U4.getProperties()){
				
				String[] pslider = this.ConvertPropertytoSlider(p,p.getConversionValue());
				JLabel label = new JLabel(p.getDescription());
				controls.add(label);
				
				JSlider slider = new JSlider(Integer.parseInt(pslider[0]),Integer.parseInt(pslider[1]),Integer.parseInt(pslider[2]));
				slider.setMinorTickSpacing(10);
				slider.addChangeListener(p);
				
				slider.setPaintLabels(true);
				controls.add(slider);
			
			}
			JPanel startstopPanel = new JPanel();
			startstopPanel.setLayout(new GridLayout(11, 0));
			JButton startB = new JButton("Start");
			startB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U4.start();
				}
			});
			JButton stopB = new JButton ("Stop");
			stopB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U4.stop();
				}
			});
			JButton resetB = new JButton ("Reset");
			resetB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					U4.reset();
					
				}
			});
			startstopPanel.add(startB);
			startstopPanel.add(stopB);
			startstopPanel.add(resetB);
			controls.add(startstopPanel);
			controls.add(Box.createVerticalGlue());
		}
	}
	
	public void addControlsU5(){
		if (U5 !=null){
			for(DoubleProperty p : U5.getProperties()){
				
				String[] pslider = this.ConvertPropertytoSlider(p,p.getConversionValue());
				JLabel label = new JLabel(p.getDescription());
				controls.add(label);
				
				JSlider slider = new JSlider(Integer.parseInt(pslider[0]),Integer.parseInt(pslider[1]),Integer.parseInt(pslider[2]));
				slider.setMinorTickSpacing(10);
				slider.addChangeListener(p);
				
				slider.setPaintLabels(true);
				controls.add(slider);
				
				
			
			}
			JPanel startstopPanel = new JPanel();
			startstopPanel.setLayout(new GridLayout(11, 0));
			JButton startB = new JButton("Start");
			startB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U5.startAnim();
				}
			});
			JButton stopB = new JButton ("Stop");
			stopB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U5.stopAnim();
				}
			});
			final JButton modeSwitch = new JButton("3D");
			modeSwitch.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (U5.getMode() == Mode._2D){
						U5.modeSwitch(Mode._3D);
						modeSwitch.setText("2D");
						DoubleProperty[] pro = U5.getProperties();
						pro[0].setCurrent(15d);
						pro[1].setCurrent(30d);
						
					}else{
						U5.modeSwitch(Mode._2D);
						modeSwitch.setText("3D");
						DoubleProperty[] pro = U5.getProperties();
						pro[0].setCurrent(0d);
						pro[1].setCurrent(0d);
					}
				}
			});
			startstopPanel.add(startB);
			startstopPanel.add(stopB);
			startstopPanel.add(modeSwitch);
			controls.add(startstopPanel);
		}
	}
	public void addControlsU6(){
		if (U6 !=null){
			for(DoubleProperty p : U6.getProperties()){
				
				String[] pslider = this.ConvertPropertytoSlider(p,p.getConversionValue());
				JLabel label = new JLabel(p.getDescription());
				controls.add(label);
				
				JSlider slider = new JSlider(Integer.parseInt(pslider[0]),Integer.parseInt(pslider[1]),Integer.parseInt(pslider[2]));
				slider.setMinorTickSpacing(10);
				slider.addChangeListener(p);
				
				slider.setPaintLabels(true);
				controls.add(slider);
				
				
			
			}
			JPanel startstopPanel = new JPanel();
			startstopPanel.setLayout(new GridLayout(11, 0));
			JButton startB = new JButton("Start");
			startB.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U6.start();
				}
			});
			JButton stopB = new JButton ("Stop");
			stopB.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					U6.stop();
				}
			});
			JButton tpraez = new JButton("Toggle Praezession");
			tpraez.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U6.togglePrecision();
				}
			});
			JButton tlight = new JButton("Toggle Light");
			tlight.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					U6.toggleLightning();
				}
			});
			startstopPanel.add(startB);
			startstopPanel.add(stopB);
			startstopPanel.add(tpraez);
			//startstopPanel.add(tlight);
			controls.add(startstopPanel);
		}
	}
}
